package example.segment;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.LockSupport;

public class Demo2 {
    volatile boolean hasSizeThread = false;
    volatile int a = 0;
    volatile int b = 0;
    volatile int c = 0;
    int sum = 0;
    AtomicInteger lockUseCount = new AtomicInteger(0);


    final Object a_lock = new Object();
    final Object b_lock = new Object();
    final Object c_lock = new Object();

    public int getSum() {
        return a + b + c;
    }

    public int getSumSync() {
        while (lockUseCount.get() != 0) {
        }
        synchronized (a_lock) {
            sum += a;
        }
        synchronized (b_lock) {
            sum += b;
        }
        synchronized (c_lock) {
            sum += c;
        }
        return sum;
    }

    public void aAdd() {
        while (!hasSizeThread) {
        }
        synchronized (a_lock) {
            lockUseCount.incrementAndGet();
            ++a;
        }
        lockUseCount.decrementAndGet();
    }


    public void bAdd() {
        synchronized (b_lock) {
            lockUseCount.incrementAndGet();
            ++b;
        }
        lockUseCount.decrementAndGet();
    }


    Thread athread;
    Thread bthread;

    public void aAdd2() {
        athread = Thread.currentThread();
        //先让b线程获取到b锁
        LockSupport.park();
        synchronized (a_lock) {
            System.out.println(Thread.currentThread().getName() + "拿到--a锁");
            LockSupport.unpark(bthread);
            System.out.println(Thread.currentThread().getName() + "再去拿b锁");
            synchronized (b_lock) {
                try {
//                    TimeUnit.SECONDS.sleep(3);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                b++;
            }
            ++a;
        }
    }

    public void bAdd2() {
        synchronized (b_lock) {
            System.out.println(Thread.currentThread().getName() + "拿到--b锁");
            bthread = Thread.currentThread();
            //唤醒
            LockSupport.unpark(athread);
            LockSupport.park();
            System.out.println(Thread.currentThread().getName() + "再去拿a锁");
            synchronized (a_lock) {
                try {
//                    TimeUnit.SECONDS.sleep(3);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                a++;
            }
            ++b;
        }
    }


    public void cAdd() {
        synchronized (c_lock) {
            lockUseCount.incrementAndGet();
            ++c;
        }
        lockUseCount.decrementAndGet();
    }

    public static void main(String[] args) throws Exception {
        Demo2 demo = new Demo2();
//        int count = 500;
//        CountDownLatch latch = new CountDownLatch(count);
//        CountDownLatch main = new CountDownLatch(count);
//        for (int i = 0; i < count; i++) {
//            new Thread(() -> {
//                try {
//                    latch.await();
//                } catch (InterruptedException e) {
//                }
//                for (int j = 0; j < 20; j++) {
//                    demo.aAdd();
//                }
//                main.countDown();
//            }).start();
//            latch.countDown();
//        }
//        main.await();
//        System.out.println(demo.a);

        new Thread(demo::aAdd2, "aaa").start();
        new Thread(demo::bAdd2, "bbb").start();
    }
}
